package com.huan.reflect;


import org.junit.Test;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class TestCar {
    //1. 获取lass对象的多种方法
    @Test
    public void test01() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        //1. 类名.class
        Class cla = Car.class;
        //2. 对象.getClass()
        Class cla1 = new Car().getClass();
        //3. Class.forName("全路径")
        Class cla2 = Class.forName("com.huan.reflect.Car");

        //实例化
        Car car = (Car) cla2.getDeclaredConstructor().newInstance();
        System.out.println(car);
    }
    //2. 获取构造方法
    @Test
    public void test02() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class cla = Car.class;
        //获取所有构造
        //getConstructors() 获取所有public构造方法
        //getDeclaredConstructors() 获取所有构造方法 包括private
        Constructor[] constructors = cla.getDeclaredConstructors();
        for (Constructor c:constructors) {
            System.out.println("name:"+c.getName()+", paramsNum:"+c.getParameterTypes());
        }
        //指定有参构造创建对象
        //1. 构造public
        Constructor constructor = cla.getConstructor(String.class, int.class, String.class);
        Car car = (Car) constructor.newInstance("namename", 10, "red");
        //2. 构造private
        Constructor constructor1 = cla.getDeclaredConstructor(String.class, int.class, String.class);
        Car car1 = (Car) constructor1.newInstance("namename", 10, "red");
    }
    //3. 获取属性
    @Test
    public void test03() throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        Class cla = Car.class;
        Car car = (Car) cla.getConstructor().newInstance();
//        Field[] fields = cla.getFields();
        Field[] fields = cla.getDeclaredFields();
        for (Field f:fields) {
            if (f.getName().equals("name")){
                //设置允许访问
                f.setAccessible(true);
                f.set(car,"wulinghongguang");
            }
            System.out.println("f.getName() = " + f.getName());
            System.out.println("car = " + car);
        }
    }
    //4. 获取方法
    @Test
    public void test04() throws Exception{
        Car car = new Car("benci", 10, "hei");
        Class cla = car.getClass();
        // 1. public
        Method[] methods = cla.getMethods();
        for (Method m: methods){
//            System.out.println("m.getName() = " + m.getName());
            //执行某方法
            if (m.getName().equals("toString")){
                String invoke = (String) m.invoke(car);
//                System.out.println("执行了invoke："+invoke);
            }
        }
        // 2. private
        Method[] declaredMethods = cla.getDeclaredMethods();
        for (Method m: declaredMethods){
            System.out.println("m.getName() = " + m.getName());
            //执行某方法
            if (m.getName().equals("run")){
                //设置 允许进行访问
                m.setAccessible(true);
                m.invoke(car);
            }
        }
    }
}
